package com.landiao.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.landiao.common.Constant;
import com.landiao.common.utils.CodecUtils;
import com.landiao.entities.Package;
import com.landiao.entities.PackagePrice;
import com.landiao.entities.ServiceItemPrice;
import com.landiao.mapper.PackageMapper;
import com.landiao.mapper.PackagePriceMapper;
import com.landiao.mapper.ServiceItemPriceMapper;

@Service
public class PackageService extends BaseService<Package, String> {

	@Autowired
	private ServiceItemPriceMapper priceMapper;
	
	@Autowired
	private PackagePriceMapper packagePriceMapper;
	
	@Override
	protected PackageMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (PackageMapper)baseMapper;
	}

	@Override
	public void save(Package record) {
		// TODO Auto-generated method stub
		record.setState(Constant.STATE_NORMAL);
		record.setStatedate(new Date());
		this.getBaseMapper().insert(record);
		if(record.getPriceList()!=null&&record.getPriceList().size()>0){
			Map<String, PackagePrice> map = new HashMap<String, PackagePrice>();
			for(ServiceItemPrice price:record.getPriceList()){
				if(price!=null&&price.getShopGradeId()!=null){
					price.setServiceItemPriceId(CodecUtils.getUUID());
					price.setCompId(record.getCompId());
					price.setPackageId(record.getPackageId());
					
					this.priceMapper.insert(price);
					
					PackagePrice packagePri= map.get(price.getShopGradeId());
					if(packagePri!=null){
						packagePri.setVipPrice(packagePri.getVipPrice().add(price.getVipPrice()));
						packagePri.setNoVipPrice(packagePri.getNoVipPrice().add(price.getNoVipPrice()));
					}else{
						packagePri = new PackagePrice();
						packagePri.setCompId(record.getCompId());
						packagePri.setPackageId(record.getPackageId());
						packagePri.setPackagePriceId(CodecUtils.getUUID());
						packagePri.setShopGradeId(price.getShopGradeId());
						packagePri.setVipPrice(price.getVipPrice());
						packagePri.setNoVipPrice(price.getNoVipPrice());
						
						map.put(price.getShopGradeId(), packagePri);
					}
				}
			}
			for(PackagePrice packagePri : map.values()){
				this.packagePriceMapper.insert(packagePri);
			}
		}
	}

	@Override
	public void update(Package record) {
		// TODO Auto-generated method stub
		this.priceMapper.deleteByPackageId(record.getPackageId());
		this.packagePriceMapper.deleteByPackageId(record.getPackageId());
		record.setState(Constant.STATE_NORMAL);
		record.setStatedate(new Date());
		this.getBaseMapper().updateByPrimaryKey(record);
		if(record.getPriceList()!=null&&record.getPriceList().size()>0){
			Map<String, PackagePrice> map = new HashMap<String, PackagePrice>();
			for(ServiceItemPrice price:record.getPriceList()){
				if(price!=null&&price.getShopGradeId()!=null){
					price.setServiceItemPriceId(CodecUtils.getUUID());
					price.setCompId(record.getCompId());
					price.setPackageId(record.getPackageId());
					
					this.priceMapper.insert(price);
					
					PackagePrice packagePri= map.get(price.getShopGradeId());
					if(packagePri!=null){
						packagePri.setVipPrice(packagePri.getVipPrice().add(price.getVipPrice()));
						packagePri.setNoVipPrice(packagePri.getNoVipPrice().add(price.getNoVipPrice()));
					}else{
						packagePri = new PackagePrice();
						packagePri.setCompId(record.getCompId());
						packagePri.setPackageId(record.getPackageId());
						packagePri.setPackagePriceId(CodecUtils.getUUID());
						packagePri.setShopGradeId(price.getShopGradeId());
						packagePri.setVipPrice(price.getVipPrice());
						packagePri.setNoVipPrice(price.getNoVipPrice());
						
						map.put(price.getShopGradeId(), packagePri);
					}
				}
			}
			for(PackagePrice packagePri : map.values()){
				this.packagePriceMapper.insert(packagePri);
			}
		}
	}
	
	/**
	 * 逻辑删除
	 */
	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		Package pa = this.getBaseMapper().selectByPrimaryKey(id);
		pa.setState(Constant.STATE_DELETE);
		pa.setStatedate(new Date());
		this.getBaseMapper().updateByPrimaryKey(pa);
	}

	public List<ServiceItemPrice> getItemPriceByPackageId(String id){
		return this.priceMapper.selectByPackageId(id);
	}
	
	public List<PackagePrice> getPackagePriceByPackageId(String id){
		return this.packagePriceMapper.selectByPackageId(id);
	}
	
	public Package getOne(Package record){
		List<Package> list = this.getBaseMapper().queryByPage(record, null);
		if(list==null||list.size()==0)
			return null;
		else
			return list.get(0);
	}
	
	public List<PackagePrice> getPackagePriceByShopId(String shopId){
		return this.packagePriceMapper.selectByShopId(shopId);
	}
}
